﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using HA.ModFlow.Domain.DomainLayer;
namespace HA.ModFlow.Controls.Windows
{
    /// <summary>
    /// winHorizontalFlowBarrier.xaml 的交互逻辑
    /// </summary>
    public partial class winHorizontalFlowBarrier : Window
    {
        public winHorizontalFlowBarrier( List<HFBItem> hfb ,GridData gridData)
        {
            InitializeComponent();
            this.hfbs.Clear();
            foreach (HFBItem item in hfb)
            {
                this.hfbs.Add((HFBItem) item.Clone());
            }
            this._grid = gridData;


            MenuItem itemCopy = new MenuItem();
            itemCopy.Header = "复制";
            itemCopy.Command = ApplicationCommands.Copy;

            //MenuItem itemPaste2 = new MenuItem();
            //itemPaste2.Header = "粘贴";
            //itemPaste2.InputGestureText = "Ctrl+V";
            //itemPaste2.Click += new RoutedEventHandler(itemPaste2_Click);

            ContextMenu cm = new ContextMenu();
            cm.Items.Add(itemCopy);
            //cm.Items.Add(itemPaste2);          
            this.dtGrid.ContextMenu = cm;
        }
        List<HFBItem> hfbs = new List<HFBItem>();

        public List<HFBItem> Hfbs
        {
            get { return hfbs; }
            set { hfbs = value; }
        }

        GridData _grid;
        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            this.dtGrid.AutoGenerateColumns = false;
            this.dtGrid.CanUserAddRows = false;
            this.dtGrid.CanUserSortColumns = false;
            DataGridTextColumn txt = new DataGridTextColumn();
            txt.Header = "层";
            Binding bin = new Binding("Layer");
            txt.Binding = bin;
            this.dtGrid.Columns.Add(txt );

            txt = new DataGridTextColumn();
            txt.Header = "行1";
            bin = new Binding("IROW1");
            txt.Binding = bin;
            this.dtGrid.Columns.Add(txt);

            txt = new DataGridTextColumn();
            txt.Header = "列1";
            bin = new Binding("ICOL1");
            txt.Binding = bin;
            this.dtGrid.Columns.Add(txt);

            txt = new DataGridTextColumn();
            txt.Header = "行2";
            bin = new Binding("IROW2");
            txt.Binding = bin;
            this.dtGrid.Columns.Add(txt);

            txt = new DataGridTextColumn();
            txt.Header = "列2";
            bin = new Binding("ICOL2");
            txt.Binding = bin;
            this.dtGrid.Columns.Add(txt);

            txt = new DataGridTextColumn();
            txt.Header = "水力特性";
            bin = new Binding("Hydchr");
            txt.Binding = bin;
            this.dtGrid.Columns.Add(txt);

            txt = new DataGridTextColumn();
            txt.Header = "水力特性的因子";
            bin = new Binding("Factor");
            txt.Binding = bin;
            this.dtGrid.Columns.Add(txt);

            this.dtGrid.ItemsSource = this.hfbs;
        }

        private void btnOk_Click(object sender, RoutedEventArgs e)
        {
            if (!CheckInputIJK())
            {
                return;
            }
            int p = 1;
            foreach (HFBItem item in this .hfbs)
            {
                if (item.IROW1 == item.IROW2)
                {
                    if (Math.Abs(item.ICOL1 - item.ICOL2) == 1)
                    {

                    }
                    else
                    {
                        MessageBox.Show("第"+p +"行数据的行列层数据不正确，请核查！","提示",MessageBoxButton.OK);
                        return;
                    }
                }
                else
                {
                    if (Math.Abs(item.IROW1 - item.IROW2) == 1 && item.ICOL1 == item.ICOL2)
                    {
                    }
                    else
                    {
                        MessageBox.Show("第" + p + "行数据的行列层数据不正确，请核查！", "提示", MessageBoxButton.OK);
                        return;
                    }
                }
                p++;
            }


            this.DialogResult = true;
            this.Close();
        }

        /// <summary>
        /// 检查层行列的正确定性
        /// </summary>
        /// <param name="row"></param>
        /// <param name="col"></param>
        /// <param name="layer"></param>
        /// <param name="index"></param>
        /// <param name="isIndexId"></param>
        /// <returns></returns>
        public bool CheckInputIJK( )
        {
            //int index = this._modflow.GridData.GetIndex(this._modflow.GridData.RowsCount, this._modflow.GridData.ColsCount, this._modflow.GridData.LayersCount);

            foreach (HFBItem item in this.hfbs)
            {
                if (item.IROW1 <= 0 || item.IROW1 > _grid.RowsCount)//行
                {
                    System.Windows.MessageBox.Show("不存在第 " + item.IROW1 + " 行，请核查！", "提示", System.Windows.MessageBoxButton.OK);
                    return false;
                }
                if (item.IROW2 <= 0 || item.IROW2 > _grid.RowsCount)//行
                {
                    System.Windows.MessageBox.Show("不存在第 " + item.IROW2 + " 行，请核查！", "提示", System.Windows.MessageBoxButton.OK);
                    return false;
                }
                if (item.ICOL1 <= 0 || item.ICOL1 > this._grid.ColsCount)
                {
                    System.Windows.MessageBox.Show("不存在第 " + item.ICOL1 + " 列，请核查！", "提示", System.Windows.MessageBoxButton.OK);
                    return false;
                }
                if (item.ICOL2 <= 0 || item.ICOL2 > this._grid.ColsCount)
                {
                    System.Windows.MessageBox.Show("不存在第 " + item.ICOL2 + " 列，请核查！", "提示", System.Windows.MessageBoxButton.OK);
                    return false;
                }
                if (item.Layer <= 0 || item.Layer > _grid.LayersCount)
                {
                    System.Windows.MessageBox.Show("不存在第 " + item.Layer + " 层，请核查！", "提示", System.Windows.MessageBoxButton.OK);
                    return false;
                }
            }

            return true;
        }


        private void btnCancal_Click(object sender, RoutedEventArgs e)
        {
            this.Close();
        }

        private void dtGrid_LoadingRow(object sender, DataGridRowEventArgs e)
        {
            e.Row.Header = (e.Row.GetIndex() + 1).ToString();
        }
    }
}
